package com.ruoyi.advertising.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.advertising.mapper.GroupPurchaseMapper;
import com.ruoyi.advertising.domain.GroupPurchase;
import com.ruoyi.advertising.service.IGroupPurchaseService;

/**
 * 优惠团购Service业务层处理
 *
 * @author mirage
 * @date 2024-09-14
 */
@Service
public class GroupPurchaseServiceImpl implements IGroupPurchaseService {
    @Autowired
    private GroupPurchaseMapper groupPurchaseMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询优惠团购
     *
     * @param id 优惠团购主键
     * @return 优惠团购
     */
    @Override
    public GroupPurchase selectGroupPurchaseById(Long id) {
        return groupPurchaseMapper.selectGroupPurchaseById(id);
    }

    /**
     * 查询优惠团购列表
     *
     * @param groupPurchase 优惠团购
     * @return 优惠团购
     */
    @Override
    public List<GroupPurchase> selectGroupPurchaseList(GroupPurchase groupPurchase) {
        return groupPurchaseMapper.selectGroupPurchaseList(groupPurchase);
    }

    /**
     * 新增优惠团购
     *
     * @param groupPurchase 优惠团购
     * @return 结果
     */
    @Override
    public int insertGroupPurchase(GroupPurchase groupPurchase) {
        groupPurchase.setStatus(0L);
        groupPurchase.setShelves(1L);
        groupPurchase.setTime(new Date());
        groupPurchase.setDeleted(0L);
        int res = groupPurchaseMapper.insertGroupPurchase(groupPurchase);
        Date starTime = groupPurchase.getTime();
        Date endTime = groupPurchase.getEndTime();
        long time = endTime.getTime() - starTime.getTime();
        rabbitTemplate.convertAndSend("advertising_exchange", "advertising.msg", String.valueOf(groupPurchase.getId()), new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(String.valueOf(time));
                return message;
            }
        });
        return res;
    }

    /**
     * 修改优惠团购
     *
     * @param groupPurchase 优惠团购
     * @return 结果
     */
    @Override
    public int updateGroupPurchase(GroupPurchase groupPurchase)
    {
        return groupPurchaseMapper.updateGroupPurchase(groupPurchase);
    }

    /**
     * 批量删除优惠团购
     *
     * @param ids 需要删除的优惠团购主键
     * @return 结果
     */
    @Override
    public int deleteGroupPurchaseByIds(Long[] ids) {
        return groupPurchaseMapper.deleteGroupPurchaseByIds(ids);
    }

    /**
     * 删除优惠团购信息
     *
     * @param id 优惠团购主键
     * @return 结果
     */
    @Override
    public int deleteGroupPurchaseById(Long id) {
        return groupPurchaseMapper.deleteGroupPurchaseById(id);
    }
}
